$(function () {

    //图片
    initFileInput("fileupload2", urlcore + "/api/attachment/upload", "#picturesUrl");
    //详情说明
    initMyCont("goodsIntroduce");

    if(location.href.indexOf("?id=")>-1){
        loadDate(location.href.split("?id=")[1]);
    }

});

//修改进入加载数据
function loadDate(id){
    $.ajax({
        type: "GET",
        url: urlcore + "/api/find/selectOne?id="+id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data:{},
        success: function (data) {
            $(".note-placeholder").hide();//隐藏添加介绍
            $("#jiu_picture_div").show();
            $("#picUrl").html("<img id=\"picImg\" src=\""+data.data.pictureUrl+"\" />");
            if (data.success == true) {
                //location.href = "find_list.html";
                $("#title").val(data.data.title);//标题
                $("#goodsId").val(data.data.goodsId);//商品
                $("#picturesUrl").val(data.data.pictureUrl);//图片ID
                $(".panel-body").html(data.data.content);//内容
                $("#bt1").hide();//保存（onSave）
                $("#bt2").show();//保存(onEdit)
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("数据异常");
        }
    });
}

function  saveOrUpdate() {
    if(location.href.indexOf("?id=")>-1){
        onEdit(location.href.split("?id=")[1]);
    }else{
        onSave();
    }
}

//保存
function onEdit(id) {
    var title = $("#title").val();
    if(title == ""){
        alert("请输入标题");
        return;
    }
    if($("#goodsId").val() == ""){
        alert("请输入商品ID");
        return;
    }
    var flag = true;
    // banner组图图片地址
    var picturesUrl = "";
    $("#picturesUrl img").each(function (index, item) {
        if (index == 0) {
            picturesUrl = $(this).attr("src");
        }else{
            alert("只能上传一张图片");
            flag = false;
            return;
        }

    });

    if(picturesUrl == ""){
        picturesUrl = $("#picImg").attr("src");
    }
    var goodsIntroduce = $('#goodsIntroduce').summernote('code');
    if(goodsIntroduce == ""){
        alert("请输入内容！");
        return;
    }
    if(flag) {

        $.ajax({
            type: "POST",
            url: urlcore + "/api/find/update",
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: JSON.stringify({
                id:id,
                title: title,
                content: goodsIntroduce,
                pictureUrl: picturesUrl,
                goodsId: $("#goodsId").val()
            }),
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    location.href = "find_list.html";
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("保存失败");
            }
        });
    }
}









//保存
function onSave() {
    var title = $("#title").val();
    if(title == ""){
        alert("请输入标题");
        return;
    }
    if($("#goodsId").val() == ""){
        alert("请输入商品ID");
        return;
    }
    var flag = true;
    // banner组图图片地址
    var picturesUrl = "";
    $("#picturesUrl img").each(function (index, item) {
        if (index == 0) {
            picturesUrl = $(this).attr("src");
        }else{
            alert("只能上传一张图片");
            flag = false;
            return;
        }

    });

    if(picturesUrl == ""){
        alert("请上传图片！")
        return;
    }
    var goodsIntroduce = $('#goodsIntroduce').summernote('code');
    if(goodsIntroduce == ""){
        alert("请输入内容！");
        return;
    }
    if(flag) {

        $.ajax({
            type: "POST",
            url: urlcore + "/api/find/add",
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: JSON.stringify({
                title: title,
                content: goodsIntroduce,
                pictureUrl: picturesUrl,
                goodsId: $("#goodsId").val()
            }),
            success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                if (data.success == true) {
                    location.href = "find_list.html";
                } else {
                    alert(data.msg);
                }
            },
            error: function () {
                alert("保存失败");
            }
        });
    }
}


/*$(document).ready(function () {
    $('.summernote').summernote({
        lang: 'zh-CN'
    });
    //summernote赋值
    $('.summernote').code("123");
    //summernote取值
	var str = $('.summernote').code();

});*/

function initMyCont(summernoteId) {
    $('#' + summernoteId).summernote({
        // height: 200,
        minHeight: 600,
        // maxHeight: 719,
        // focus: true,
        placeholder: '添加介绍...',
        lang: 'zh-CN',
        dialogsInBody: true,
        disableDragAndDrop: false,
        popover: {
            image: [
                ['imagesize', ['imageSize100', 'imageSize50', 'imageSize25']],
                ['float', ['floatLeft', 'floatRight', 'floatNone']],
                ['remove', ['removeMedia']]
            ],
            link: [
                ['link', ['linkDialogShow', 'unlink']]
            ],
            air: [
                ['color', ['color']],
                ['font', ['bold', 'underline', 'clear']],
                ['para', ['ul', 'paragraph']],
                ['table', ['table']],
                ['insert', ['link', 'picture']]
            ]
        },
        callbacks: {
            onImageUpload: function (files, editor, $editable) {
                sendFile(files, editor, $editable, summernoteId);
            },
            onPaste: function(e) {
                // alert('Called event paste');
            }
        }
    });
}

function sendFile(files, editor, $editable, summernoteId) {
    var data = new FormData();
    data.append("file", files[0]);
    $.ajax({
        data: data,
        type: "POST",
        url: urlcore + "/api/attachment/upload", //图片上传出来的url，返回的是图片上传后的路径，http格式
        cache: false,
        contentType: false,
        processData: false,
        dataType: "json",
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            $('#' + summernoteId).summernote('insertImage', data.data[0]); // the insertImage API
        },
        error: function () {
            alert("上传失败");
        }
    });
}